home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QRZ! Ham Radio 8
/
QRZ Ham Radio Callsign Database - Volume 8.iso
/
mac
/
files
/
infodata
/
callbook.tar
/
callbook_1.3
/
lookup.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-03-31
|
2KB
|
105 lines
/*
* Telnet callsign server sources copyright 1989 by Devon Bowen, KA2NRC.
* You may distribute and modify these files as you please as long as
* as long as credit to the original creator is given. Please report any
* bug fixes or modification to bowen@cs.buffalo.edu.
*/
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include "callbook.h"
extern int output;
char *index(), *my_fgets(), *cvtdate();
unsigned short strhash();
lookup(field, name, printer)
int field;
char *name;
int (*printer)();
{
FILE *fd;
int fi, position, i, found_one=0;
struct stat indata;
int recl, recf, recn;
unsigned short hashnum, hash;
char searchfile[BUFSIZ];
char datafile[BUFSIZ];
char buf[256], *search;
hash = strhash(name);
sprintf(searchfile, "%scallsign.%d.search", DB_DIR, field);
sprintf(datafile, "%scallsign.%d.data", DB_DIR, field);
stat(searchfile, &indata);
recf = 0;
recl = (indata.st_size / 6) - 1;
recn = (recl-recf) / 2 + recf;
fi = open(searchfile, O_RDONLY);
(void) lseek(fi, recn*6, L_SET);
read(fi, &hashnum, sizeof(hashnum));
while (hashnum != hash) {
if (hashnum < hash)
recf = recn+1;
if (hashnum > hash)
recl = recn-1;
if (recl < recf)
return 0;
recn = (recl-recf) / 2 + recf;
(void) lseek(fi, recn*6, L_SET);
read(fi, &hashnum, sizeof(hashnum));
}
read(fi, &position, sizeof(position));
close(fi);
fd = fopen(DB_TEXT, "r");
fi = open(datafile, O_RDONLY);
(void) lseek(fi, position, L_SET);
read(fi, &position, sizeof(position));
while (position != -1) {
fseek(fd, position, 0);
search = my_fgets(buf, 255, fd);
for (i=0; i<field; i++)
search = index(search, '|') + 1;
while (*search == ' ')
search++;
if (!strncasecmp(search, name, strlen(name)) && filter(buf))
if (printer(buf, found_one++))
break;
read(fi, &position, sizeof(position));
}
close(fi);
fclose(fd);
if (found_one)
return 1;
else
return 0;
}